package com.jasonzhou.onetomanycontrol.base;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;

import com.jasonzhou.onetomanycontrol.R;
import com.jasonzhou.onetomanycontrol.dialog.TipsDialog;


/**
 * Created by android on 2017/11/13.
 */

public abstract class BaseFragment extends Fragment implements FragmentUserVisibleController.UserVisibleCallback {
    public static final String ARGUMENT = "argument";
    protected final String TAG = "TAG-A";
    private int statusHeight;
    protected static Resources resources;
    protected boolean isOpenKeyListener = false;

    public static long SEND_INTERVAL = 200L;
    protected long lastRecordSendTime = 0L;

    protected TipsDialog tipsDialog = new TipsDialog();

    protected FragmentUserVisibleController controller;

    {
        controller = new FragmentUserVisibleController(this, this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        controller.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        controller.activityCreated();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EventBus.getDefault().register(this);
        resources = this.getResources();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//        if (presenter == null) {
//            throw new NullPointerException("presenter 不能为空!");
//        }

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        controller.resume();
        if (isOpenKeyListener) {
            getView().setFocusableInTouchMode(true);
            getView().requestFocus();
            getView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            onBack();
                            return true;
                        }
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        controller.pause();
    }

    protected void startActivitys(Intent intent) {
        startActivity(intent);
    }

    protected void startActivitys(Intent intent, int enterAnim, int exitAnim) {
        startActivity(intent);
        this.getActivity().overridePendingTransition(enterAnim, exitAnim);
    }

    protected void onBack() {
    }

    protected void openKeyListener() {
        isOpenKeyListener = true;
    }

    protected void closeKeyListener() {
        isOpenKeyListener = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }


    public int getmColor(int resId) {
        return ContextCompat.getColor(getActivity(), resId);
    }


    /**
     * 界面可见时
     */
    protected void onVisible() {

    }

    /**
     * 界面不可见时
     */
    protected void inVisible() {

    }

    public void showView() {

    }

    public static <T extends BaseFragment> T newInstance(Class<T> tClass, String argument) {
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT, argument);
        T t = null;
        try {
            t = tClass.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        if (t != null) {
            t.setArguments(bundle);
        }
        return t;
    }

    /************************************************************************/
    @Override
    public Lifecycle getLifecycle() {
        return super.getLifecycle();
    }

    @Override
    public boolean isWaitingShowToUser() {
        return controller.isWaitingShowToUser();
    }

    @Override
    public void setWaitingShowToUser(boolean waitingShowToUser) {
        controller.setWaitingShowToUser(waitingShowToUser);
    }

    @Override
    public boolean isVisibleToUser() {
        return controller.isVisibleToUser();
    }

    @Override
    public void callSuperSetUserVisibleHint(boolean isVisibleToUser) {
        setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onVisibleToUserChanged(boolean isVisibleToUser, boolean invokeInResumeOrPause) {
        if (isVisibleToUser) {
            onVisible();
        } else {
            inVisible();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        controller.onHiddenChanged(hidden);
    }

    public Fragment mCurFragment;
    protected void switchCenter(Class<? extends Fragment> clazz) {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment userFragment = fm.findFragmentByTag(clazz.getName());
        if (userFragment == null) {
            try {
                userFragment = clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException | java.lang.InstantiationException e) {
                e.printStackTrace();
            }
        }
        if (mCurFragment != null && mCurFragment != userFragment) {
            ft.hide(mCurFragment);
        }
        if (!userFragment.isAdded()) {
            ft.add(R.id.main_content, userFragment, clazz.getName());
        } else {
            ft.show(userFragment);
        }
        ft.commitAllowingStateLoss();
        mCurFragment = userFragment;
    }

}
